package com.cars.bim.generator.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cars.bim.generator.entity.TbKaoqinEntity;
import com.cars.bim.generator.entity.TbLeaveEntity;
import com.cars.bim.generator.entity.TbOvertimeEntity;
import com.cars.bim.generator.service.TbDakaService;
import com.cars.bim.generator.service.TbKaoqinService;
import com.cars.bim.generator.service.TbLeaveService;
import com.cars.bim.generator.service.TbOvertimeService;
import com.cars.framework.common.utils.PageUtils;
import com.cars.framework.common.utils.Query;
import com.cars.framework.common.utils.R;
import com.cars.framework.common.utils.ShiroUtils;
import com.cars.framework.modules.sys.entity.SysUserEntity;
import com.cars.framework.modules.sys.service.SysUserService;




/**
 * 
 * 
 * @author admin
 * @email admin@gmail.com
 * @date 2018-04-22 19:52:15
 */
@RestController
@RequestMapping("/generator/tbkaoqin")
public class TbKaoqinController {
	@Autowired
	private TbKaoqinService tbKaoqinService;
	
	@Autowired
	private TbDakaService tbDakaService;
	
	@Autowired
	private TbLeaveService tbLeaveService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private TbOvertimeService tbOvertimeService;
	
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:tbkaoqin:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		 Query query = null;
	        SysUserEntity  user =  	ShiroUtils.getUserEntity();
	        if(!"admin".equals(user.getUsername())){
	        	
	        	params.put("name",user.getUsername());
	        }
	        query = new Query(params);

		List<TbKaoqinEntity> tbKaoqinList =new ArrayList<TbKaoqinEntity>() ;
		int total = tbKaoqinService.queryTotal(query);
		
	
		
		
		List<Map> kaoMapList = tbDakaService.queryCount();
		if(kaoMapList!=null &&kaoMapList.size()>0){
			for(int i=0;i<kaoMapList.size();i++){
				TbKaoqinEntity tbKaoqin = new TbKaoqinEntity();
				Map map = kaoMapList.get(i);
				
				SysUserEntity sysUser = sysUserService.queryByUserName((String)map.get("name"));
				
				TbLeaveEntity tbLeaveEntity = new TbLeaveEntity();
			    tbLeaveEntity.setUserId((String)map.get("name"));
			    tbLeaveEntity.setBeginMonth((String)map.get("sYear")+"-"+(String)map.get("sMonth"));
				
			    tbLeaveEntity = tbLeaveService.queryCountByNameMonth(tbLeaveEntity);
			    
			    
			    double  gongzi = Double.parseDouble(sysUser.getGongzi());
			    long count = (Long)map.get("count");
				double chuqin =  Double.parseDouble(count+"");
				if(chuqin!= 22){
					gongzi = (gongzi/22)*chuqin;
				}
				TbOvertimeEntity tbOvertimeEntity = new TbOvertimeEntity();
				tbOvertimeEntity.setBeginMonth((String)map.get("sYear")+"-"+(String)map.get("sMonth"));
				tbOvertimeEntity.setUserId((String)map.get("name"));
				tbOvertimeEntity = tbOvertimeService.queryCountByNameMonth(tbOvertimeEntity);
				
				double jiabantotal = 0;
				double qingjiatotal =0 ;
				
				if(tbOvertimeEntity!=null)
				{
					tbKaoqin.setNianxiu(tbOvertimeEntity.getTotalDate()+"");
					jiabantotal = tbOvertimeEntity.getTotalDate()*20; //加班20
				}else
				{
					tbKaoqin.setNianxiu("0");
				}
						
				if(tbLeaveEntity!=null)
				{
					tbKaoqin.setQingjia(tbLeaveEntity.getTotalDate()+"");
					qingjiatotal = tbLeaveEntity.getTotalDate()*100; //一天扣100
				}else
				{
					tbKaoqin.setQingjia("0");
				}
				double  g= gongzi-qingjiatotal+jiabantotal;
				tbKaoqin.setBeginDate((String)map.get("sYear")+"-"+(String)map.get("sMonth"));
				tbKaoqin.setChuqin(chuqin+"");
			
				tbKaoqin.setUsername((String)map.get("name"));
				tbKaoqin.setCreateDate(g+"");
				tbKaoqinList.add(tbKaoqin);
			    
			}
		}
		PageUtils pageUtil = new PageUtils(tbKaoqinList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		TbKaoqinEntity tbKaoqin = tbKaoqinService.queryObject(id);
		
		return R.ok().put("tbKaoqin", tbKaoqin);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("generator:tbkaoqin:save")
	public R save(@RequestBody TbKaoqinEntity tbKaoqin){
		
		tbKaoqinService.save(tbKaoqin);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("generator:tbkaoqin:update")
	public R update(@RequestBody TbKaoqinEntity tbKaoqin){
		tbKaoqinService.update(tbKaoqin);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:tbkaoqin:delete")
	public R delete(@RequestBody Integer[] ids){
		tbKaoqinService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
